package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dto.DishDto;
import com.pojo.Category;
import com.pojo.Dish;
import com.pojo.DishFlavor;
import com.service.CategoryService;
import com.service.DishFlavorService;
import com.service.DishService;
import com.util.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;


    @GetMapping("/page")
    public R<Page> page( int page,int pageSize, String name){
        Page<Dish> page1 = new Page<>(page, pageSize);
        Page<DishDto> page2 = new Page<>();
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper();
        wrapper.like(Strings.isNotEmpty(name),Dish::getName,name);
        wrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(page1, wrapper);
        BeanUtils.copyProperties(page1,page2,"records");
        List<Dish> list = page1.getRecords();
        List<DishDto> list1=list.stream().map((item)->{
            DishDto dto = new DishDto();
            BeanUtils.copyProperties(item,dto);
            Long id = item.getCategoryId();
            Category byId = categoryService.getById(id);
            String s = byId.getName();
            dto.setCategoryName(s);
            return dto;
        }).collect(Collectors.toList());
        page2.setRecords(list1);
        return R.success(page2);
    }

    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> getByid(@PathVariable Long id){
        DishDto flavor = dishService.getByIdWithFlavor(id);
        return R.success(flavor);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    /*@GetMapping("/{list}")
    public R<Dish> getDish(String categoryId){
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Strings.isNotEmpty(categoryId),Dish::getCategoryId,categoryId);
        Dish one = dishService.getOne(wrapper);
        return R.success(one);
    }*/

    @DeleteMapping
    public R<String> delete(@RequestParam(value = "ids") List<Long> ids){
        dishService.removeBatchByIds(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{sta}")
    public R<String> status(@PathVariable int sta,@RequestParam(value = "ids") List<Long> ids){
        Dish dish = new Dish();
        for (Long dishId : ids) {
            dish.setId(dishId);
            dish.setStatus(sta);
            dishService.updateById(dish);
        }
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> getCateory(Long categoryId,int status){
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId);
        wrapper.eq(Dish::getStatus,status);
        List<Dish> list = dishService.list(wrapper);
        /*DishDto dto = new DishDto();
        BeanUtils.copyProperties(list,dto);
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DishFlavor::getDishId,dto.getId());
        List<DishFlavor> list1 = dishFlavorService.list(wrapper1);
        dto.setFlavors(list1);*/
        List<DishDto> list1=list.stream().map((item)->{
            DishDto dto = new DishDto();
            BeanUtils.copyProperties(item,dto);
            Long id = item.getCategoryId();
            Category byId = categoryService.getById(id);
            String s = byId.getName();
            dto.setCategoryName(s);
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,item.getId());
            List<DishFlavor> list2 = dishFlavorService.list(wrapper1);
            dto.setFlavors(list2);
            return dto;
        }).collect(Collectors.toList());
        return R.success(list1);
    }
}
